package com.woniuxy.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.base.mapper.BaseProductLineMapper;
import com.woniuxy.base.model.entity.BaseProductLine;
import com.woniuxy.base.model.entity.BaseProductLineRecord;
import com.woniuxy.base.service.BaseProductLineRecordService;
import com.woniuxy.base.service.BaseProductLineService;
import com.woniuxy.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BaseProductLineServiceImpl implements BaseProductLineService {
    @Resource
    private BaseProductLineMapper baseProductLineMapper;

    @Resource
    private BaseProductLineRecordService recordService;

    @Override
    public Page<BaseProductLine> getAll(int current, int size) {
        return baseProductLineMapper.selectPage(new Page<>(current, size),null);
    }

    @Transactional
    @Override
    public Boolean update(BaseProductLine baseProductLine, String account) {
        BaseProductLine record = baseProductLineMapper.selectById(baseProductLine.getId());
        BaseProductLineRecord productLineRecord = new BaseProductLineRecord();
        productLineRecord.setProducter(record.getProducter())
                        .setState(record.getState()).setProductLineId(record.getId())
                        .setDescription(record.getDescription()).setUpdateTime(record.getAddTime())
                        .setProductLineId(record.getId()).setUpdatePerson(account);
        baseProductLineMapper.update(baseProductLine, new UpdateWrapper<BaseProductLine>().eq("id",baseProductLine.getId()));
        recordService.add(productLineRecord);
        return true;
    }

    @Override
    public Boolean add(BaseProductLine baseProductLine) {
        baseProductLine.setCode(String.valueOf(SnowflakeIdWorker.newId()));
        baseProductLineMapper.insert(baseProductLine);
        return true;
    }

    @Override
    public Page<BaseProductLine> conditionSearch(BaseProductLine baseProductLine, Integer current, Integer size) {
        return baseProductLineMapper.selectPage(new Page<>(current,size),new QueryWrapper<BaseProductLine>(baseProductLine));
    }

    @Override
    public Page<BaseProductLine> keywordSearch(String keyword, Integer current, Integer size) {
        QueryWrapper<BaseProductLine> wrapper = new QueryWrapper<>();
        wrapper.like("producter",keyword).or().like("description",keyword);
        return baseProductLineMapper.selectPage(new Page<>(current,size),wrapper);
    }

    @Override
    public List<BaseProductLineRecord> detail(Integer productLineId) {
        return recordService.detail(productLineId);
    }

}
